function [filtered_data] = FUN_plotDataFilter(fig,data )
%FUN_plotDataFreq Summary of this function goes here
%   Detailed explanation goes here

%% Data preparation and preproccess
% sample time and frequency
% generate time and frequency sequence
size = length(data);
sampletime = 0.01;                                  %sample time that unit is second
ts = sampletime;
t = 0:sampletime:(size-1) * sampletime;             %time point
samplefreq = 1/sampletime;                          %sample frequency that unit is Hz
fs = samplefreq;
f = (0:(size-1))/size*samplefreq;                   %frequency point
FontSize = 12;   %12~16
%% orignal data analysis
fft_data=fft(data);
fft_data_abs=abs(fft_data);

figure(fig);clf();subplot(2,2,1);
plot(t,data,'k');
title('Time Domain Analysis');
xlabel('Time(s)'); ylabel('Amplitude')
set(get(gca,'XLabel'),'FontSize',FontSize);
set(get(gca,'YLabel'),'FontSize',FontSize);

figure(fig);subplot(2,2,3);
plot(f,fft_data_abs,'k');
title('Frequency domain orignal PPG data ');
xlabel('Frequency(Hz)'); ylabel('Amplitude');
set(get(gca,'XLabel'),'FontSize',FontSize);
set(get(gca,'YLabel'),'FontSize',FontSize);

data_offset = data - data(1);
% Hd = bandpassfilter;
Hd = lowpassfilter;
filtered_data = filter(Hd,data_offset);

% flitered_data = detrend(flitered_data,'linear',30);
% data_pretrend = IIR_datafilter_success(data);
% [p,s,mu] = polyfit((1:ppg_size),data_pretrend,6);
% f_y = polyval(p,(1:ppg_size),[],mu);
% ppg_fliterdata = ppg_data_pretrend - f_y;        % Detrend data

fft_filtered_data=fft(filtered_data);
filtered_data = filtered_data + data(1);
fft_filtered_data_abs = abs(fft_filtered_data);

figure(fig);subplot(2,2,2);hold on;
plot(t,filtered_data,'b');
title('Time Domain Filtered Analysis');
xlabel('Time(s)'); ylabel('Amplitude');
set(get(gca,'XLabel'),'FontSize',FontSize);
set(get(gca,'YLabel'),'FontSize',FontSize);

figure(fig);subplot(2,2,4);
plot(f,fft_filtered_data_abs,'b');
title('Frequency Domain filtered Analysis');
xlabel('Frequency(Hz)'); ylabel('Amplitude');
set(get(gca,'XLabel'),'FontSize',FontSize);
set(get(gca,'YLabel'),'FontSize',FontSize);

end

